package com.xbf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbf.entity.User;
import com.xbf.mapper.UserMapper;
import com.xbf.service.UserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User login(String username, String password) {
        return lambdaQuery()
                .eq(User::getUName, username)
                .eq(User::getUPassword, password)
                .one();
    }


    @Override
    public Map<String, Object> register(User user) {
        Map<String, Object> result = new HashMap<>();

        // 检查用户名是否已存在
        boolean exists = lambdaQuery()
                .eq(User::getUName, user.getUName())
                .exists();

        if (exists) {
            result.put("success", false);
            result.put("message", "用户名已存在");
            return result;
        }

        // 保存新用户
        boolean saveSuccess = save(user);
        result.put("success", saveSuccess);
        result.put("message", saveSuccess ? "注册成功" : "注册失败");
        return result;
    }

    @Override
    public User getFullUserById(Integer id) {
        return getById(id);
    }
}